package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.Adsize;

/**
 * This class deals with the database operations of Adsize entity
 * 
 * @author siddharthsaha
 * 
 */
public class AdsizeDatabaseOperations {

	private static Logger logger = Logger.getLogger("dboperations");

	public void persistAdsizeEntity(Adsize adsizeEntity) {
		logger.info("IN method persistAdsizeEntity() in AdsizeDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(adsizeEntity);
		if (persistStatus == 0) {
			logger.info("The adsize entity with id: " + adsizeEntity.getAdsizeId() + " was successfully persisted");
		} else {
			logger.error("An error has occurred while persisting the Adsize entity with id: " + adsizeEntity.getAdsizeId());
		}
		logger.info("OUT method persistAdsizeEntity() in AdsizeDatabaseOperations");
	}

	public void removeAdsizeEntity(Adsize adsizeEntity) {
		logger.info("IN method removeAdsizeEntity() in AdsizeDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(adsizeEntity, adsizeEntity.getAdsizeId().intValue());
		if (removeStatus == 0) {
			logger.info("The adsize entity was successfully removed from the database");
		} else {
			logger.error("The adsize entity was not successfully removed from the database");
		}
		logger.info("OUT method removeAdsizeEntity() in AdsizeDatabaseOperations");
	}

	public void updateAdsizeEntity(Adsize adsizeEntity) {
		logger.info("IN method updateAdsizeEntity() in AdsizeDatabaseOperations");
		String query = "SELECT adsizeEntity FROM Adsize adsizeEntity where adsizeEntity.system.id = " + adsizeEntity.getSystem().getId()
				+ " AND adsizeEntity.adsizeRefCode = '" + adsizeEntity.getAdsizeRefCode() + "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int index = 0; index < retrivedEntities.size(); index++) {
			Adsize currentAdsizeEntity = (Adsize) retrivedEntities.get(index);
			if (currentAdsizeEntity.getSystem().getId().intValue() == adsizeEntity.getSystem().getId().intValue()
					&& currentAdsizeEntity.getAdsizeRefCode().equals(adsizeEntity.getAdsizeRefCode())) {
				logger.info("Obtained the adsize entity to be updated");
				currentAdsizeEntity.setAdsizeCode(adsizeEntity.getAdsizeCode());
				currentAdsizeEntity.setDescription(adsizeEntity.getDescription());
				currentAdsizeEntity.setHeight(adsizeEntity.getHeight());
				currentAdsizeEntity.setMediatype(adsizeEntity.getMediatype());
				currentAdsizeEntity.setStatus(adsizeEntity.getStatus());
				currentAdsizeEntity.setWidth(adsizeEntity.getWidth());
				int updateStatus = databaseAccessService.updateEntity(currentAdsizeEntity);
				if (updateStatus == 0) {
					updated = true;
				}
				break;
			}
		}
		if (updated) {
			logger.info("The adsize entity was successfully updated");
		} else {
			logger.error("The adsize entity was not successfully updated");
		}
		logger.info("OUT method updateAdsizeEntity() in AdsizeDatabaseOperations");
	}

	public List<Adsize> getAllAdsizeEntites() {
		logger.info("IN method getAllAdsizeEntities() in AdsizeDatabaseOperations");
		List<Adsize> toReturnList = new ArrayList<Adsize>();
		String query = "SELECT adsizeEntity FROM Adsize adsizeEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			Adsize currentAdsizeEntity = (Adsize) retrievedEntities.get(index);
			toReturnList.add(currentAdsizeEntity);
		}
		logger.info("OUT method getAllAdsizeEntities() in AdsizeDatabaseOperations");
		return toReturnList;
	}

	public List<Adsize> getAdsizeEntitiesByAdsizeId(int adsizeId) {
		logger.info("IN method getAdsizeEntitiesByAdsizeId() in AdsizeDatabaseOperations");
		List<Adsize> toReturnList = new ArrayList<Adsize>();
		String query = "SELECT adsizeEntity FROM Adsize adsizeEntity WHERE adsizeEntity.adsizeId = " + adsizeId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedEntities.size(); index++) {
			Adsize currentAdsizeEntity = (Adsize) retrievedEntities.get(index);
			if (currentAdsizeEntity.getAdsizeId().intValue() == adsizeId) {
				toReturnList.add(currentAdsizeEntity);
			}
		}
		logger.info("OUt method getAdsizeEntitiesByAdsizeId() in AdsizeDatabaseOperations");
		return toReturnList;
	}

	public List<Adsize> getAdsizeEntitiesByAdsizeRefCode(String adsizeRefCode) {
		logger.info("IN method getAdsizeEntitiesByAdsizeRefCode() in AdsizeDatabaseOperations");
		List<Adsize> toReturnList = new ArrayList<Adsize>();
		String query = "SELECT adsizeEntity FROM Adsize adsizeEntity WHERE adsizeEntity.adsizeRefCode = '" + adsizeRefCode + "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedList = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedList.size(); index++) {
			Adsize currentAdsizeEntity = (Adsize) retrievedList.get(index);
			if (currentAdsizeEntity.getAdsizeRefCode().equals(adsizeRefCode)) {
				toReturnList.add(currentAdsizeEntity);
			}
		}
		logger.info("OUt method getAdsizeEntitiesByAdsizeRefCode() in AdsizeDatabaseOperations");
		return toReturnList;
	}

	public List<Adsize> getAdsizeEntitiesWithMediatypeId(int mediatypeId) {
		logger.info("IN method getAdsizeEntitiesWithMediatypeId() in AdsizeDatabaseOperations");
		List<Adsize> toReturnList = new ArrayList<Adsize>();
		String query = "SELECT adsizeEntity FROM Adsize adsizeEntity WHERE adsizeEntity.mediatype.mediatypeId = " + mediatypeId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedList = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedList.size(); index++) {
			Adsize currentAdsizeEntity = (Adsize) retrievedList.get(index);
			if (currentAdsizeEntity.getMediatype().getMediatypeId().intValue() == mediatypeId) {
				toReturnList.add(currentAdsizeEntity);
			}
		}
		logger.info("OUT method getAdsizeEntitesWithMediatypeId() in AdsizeDatabaseOperations");
		return toReturnList;
	}

}
